﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
    bool moveLeft, moveRight, moveUp, moveDown;
	// Use this for initialization
	void Start() 
    {
        moveLeft = moveRight = moveUp = moveDown = false;
	}
	
	// Update is called once per frame
	void Update() 
    {
        CheckKeyDown();
        CheckKeyUp();
        Move();
	}

    void CheckKeyDown()
    {
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            moveLeft = true;
        }

        if(Input.GetKeyDown(KeyCode.RightArrow))
        {
            moveRight = true;
        }

        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            moveUp = true;
        }

        if(Input.GetKeyDown(KeyCode.DownArrow))
        {
            moveDown = true;
        }
    }

    void CheckKeyUp()
    {
        if (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            moveLeft = false;
        }

        if (Input.GetKeyUp(KeyCode.RightArrow))
        {
            moveRight = false;
        }

        if (Input.GetKeyUp(KeyCode.UpArrow))
        {
            moveUp = false;
        }

        if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            moveDown = false;
        }
    }

    void Move()
    {
        if (moveLeft)
        {
            transform.Translate(Vector2.right * -0.1f);
            //Camera.main.transform.Translate(Vector2.right * -0.1f);
        }

        if (moveRight)
        {
            transform.Translate(Vector2.right * 0.1f);
            //Camera.main.transform.Translate(Vector2.right * 0.1f);
        }

        if (moveUp)
        {
            transform.Translate(Vector2.up * 0.1f);
            //Camera.main.transform.Translate(Vector2.up * 0.1f);
        }

        if (moveDown)
        {
            transform.Translate(Vector2.up * -0.1f);
            //Camera.main.transform.Translate(Vector2.up * -0.1f);
        }
    }

    void OnCollisionEnter2D(Collision2D people)
    {
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
            people.gameObject.SendMessage("Speak", "hahahahaha");
        //}
    }
}
